package com.keba.lamp.system.manager.application.impl;

import cn.hutool.core.collection.CollUtil;

import com.google.common.collect.Multimap;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.keba.basic.base.manager.impl.SuperManagerImpl;
import com.keba.basic.cache.repository.CacheOps;
import com.keba.basic.database.mybatis.conditions.Wraps;
import com.keba.basic.database.mybatis.conditions.query.LbQueryWrap;
import com.keba.basic.model.cache.CacheKey;
import com.keba.basic.utils.CollHelper;
import com.keba.basic.utils.StrPool;
import com.keba.lamp.common.cache.tenant.tenant.TenantResourceCacheKeyBuilder;

import com.keba.lamp.system.entity.application.DefTenantResourceRel;
import com.keba.lamp.system.manager.application.DefTenantResourceRelManager;
import com.keba.lamp.system.mapper.application.DefTenantResourceRelMapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 应用管理
 *
 * @author tangyh
 * @version v1.0
 * @date 2021/9/29 1:26 下午
 * @create [2021/9/29 1:26 下午 ] [tangyh] [初始创建]
 */
@RequiredArgsConstructor
@Service
public class DefTenantResourceRelManagerImpl extends SuperManagerImpl<DefTenantResourceRelMapper, DefTenantResourceRel> implements DefTenantResourceRelManager {
    private final CacheOps cacheOps;

    @Override
    public Map<Long, Collection<Long>> findResourceIdByTenantId(Long tenantId) {
        List<DefTenantResourceRel> list = list(Wraps.<DefTenantResourceRel>lbQ().eq(DefTenantResourceRel::getTenantId, tenantId));
        Multimap<Long, Long> map = CollHelper.iterableToMultiMap(list, DefTenantResourceRel::getApplicationId, DefTenantResourceRel::getResourceId);
        return map.asMap();
    }

    @Override
    public void deleteByTenantId(List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        LbQueryWrap<DefTenantResourceRel> wrap = Wraps.<DefTenantResourceRel>lbQ().in(DefTenantResourceRel::getTenantId, ids);
        List<DefTenantResourceRel> list = list(wrap);
        remove(wrap);

        List<CacheKey> keys = new ArrayList<>();
        Set<String> set = new HashSet<>();
        for (DefTenantResourceRel defTenantResourceRel : list) {
            String key = defTenantResourceRel.getTenantId() + StrPool.DOT + defTenantResourceRel.getApplicationId();
            if (!set.contains(key)) {
                keys.add(TenantResourceCacheKeyBuilder.builder(defTenantResourceRel.getTenantId(), defTenantResourceRel.getApplicationId()));
                set.add(key);
            }
        }
        cacheOps.del(keys);
        cacheOps.del(ids.stream().map(id -> TenantResourceCacheKeyBuilder.builder(id, null)).toArray(CacheKey[]::new));
    }
}
